package com.personal.website.lottery.web.service.impl;

import com.personal.website.lottery.web.dao.VerifyCodeMapper;
import com.personal.website.lottery.web.model.VerifyCode;
import com.personal.website.lottery.web.model.VerifyCodeExample;
import com.personal.website.lottery.web.service.VerifyCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Created by Gavin on 2016/5/7.
 */
@Service("verifyCodeService")
public class VerifyCodeServiceImpl implements VerifyCodeService {
    @Resource
    private VerifyCodeMapper verifyCodeMapper;

    public void insertVerifyCode(VerifyCode verifyCode) {
        this.verifyCodeMapper.insert(verifyCode);
    }

    public Boolean validate(String userName, String email, String verifyCode, Date endDate) {
        VerifyCodeExample example = new VerifyCodeExample();
        example.createCriteria().andUserNameEqualTo(userName).andEmailEqualTo(email)
                .andVerifyCodeEqualTo(verifyCode).andCreateTimeGreaterThanOrEqualTo(endDate);
        if(this.verifyCodeMapper.countByExample(example) > 0) {
            return true;
        }
        return false;
    }
}
